﻿using System;
using System.Collections.Generic;
using System.Web;

using LMSemir.Common;
using LMSemir.DAC;
namespace LMSemir.Backstage.Ajax
{
    /// <summary>
    /// AjaxHandler 的摘要说明
    /// </summary>
    public class AjaxHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string method = WebUtils.GetPramaValue("m", "");
            switch (method.ToLower())
            {
                case "setteestatus":
                    this.setTeeInfoById(context);
                    break;
                case "setcategory":
                    this.SetTeeCategoryById(context);
                    break;
                case "updatevotecount":
                    this.SetVoteCount(context);
                    break;
            }
        }
        private void SetVoteCount(HttpContext context)
        {
            int tid = WebUtils.GetPramaValue("tid", 0);
            int voteCount = WebUtils.GetPramaValue("votecount", 0);
            if (voteCount > 0 && tid > 0)
            {
                string flag = TeeDataAccess.SetVoteCountById(tid, voteCount) == true ? "y" : "n";
                context.Response.Write(flag);
            }
        }
        private void SetTeeCategoryById(HttpContext context)
        {
            if (null != context.Request["cid"] && null != context.Request["ids"])
            {
                bool isSuccess = false;
                int cid = Convert.ToInt32(context.Request["cid"]);
                string strTeeIDs = context.Request["ids"];
                try
                {
                    string[] strTeeIds = strTeeIDs.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    if (strTeeIds.Length > 0)
                    {
                        for (int i = 0; i < strTeeIds.Length; i++)
                        {
                            isSuccess = TeeDataAccess.SetTeeCategory(Convert.ToInt32(strTeeIds[i]), cid);
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogUtil.GetInstance().WriteDebugLog(DateTime.Now.ToString() + "|method:SetTeeCategoryById,error=>" + ex.Message);
                }
                context.Response.Write(isSuccess == true ? "y" : "n");
            }
        }
        private void setTeeInfoById(HttpContext context)
        {
            if (null != context.Request["type"] && null != context.Request["tid"])
            {
                int iTeeStatus = 0;
                int teeType = 0;
                bool isSuccess = false;
                string ids = context.Request["tid"];
                string strType = context.Request["type"];
                switch (strType.ToLower())
                {
                    case "settop20":
                        teeType = 1;
                        iTeeStatus = 1;
                        break;
                    case "settop6":
                        teeType = 2;
                        iTeeStatus = 2;
                        break;
                    case "setwin":
                        teeType = 3;
                        iTeeStatus = 3;
                        break;
                    case "setverify":
                        iTeeStatus = 4;
                        break;
                    case "deltee":
                        iTeeStatus = -1;
                        break;

                    //取消作品状态位操作
                    case "canceltop20":
                        teeType = 1;
                        iTeeStatus = 0;
                        break;
                    case "canceltop6":
                        teeType = 2;
                        iTeeStatus = 0;
                        break;
                    case "cancelwin":
                        teeType = 3;
                        iTeeStatus = 0;
                        break;
                    case "cancelsetverify":
                        teeType = 4;
                        iTeeStatus = 0;
                        break;
                }

                string[] idList = ids.Split(new string[] { "," }, StringSplitOptions.None);
                if (idList.Length > 0)
                {
                    for (int i = 0; i < idList.Length; i++)
                    {
                        isSuccess = TeeDataAccess.setTeeStatusById(Convert.ToInt32(idList[i]), teeType, iTeeStatus);
                    }
                }
                string opCode = isSuccess == true ? "y" : "n";
                context.Response.Write(opCode);
            }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}